package DataPreprocess;
import java.io.*;

public class CleanStopWords 
{
	private String filePath_stopWordlist = null;
	private int spotwordRow = 27;								// It can be expended by user, now is 26 letters + 1 symbol,
	private String stoplist[][] = null;
	public CleanStopWords(String filePath_stopWordlist, int spotwordRow)
	{
		this.filePath_stopWordlist = filePath_stopWordlist;
		this.spotwordRow = spotwordRow;
		this.stoplist = new String [spotwordRow][];
		this._001_buildStopWordTable();
	}
	private void _001_buildStopWordTable()
	{
		int count = 0;
		String str = null;
		BufferedReader sReader = null;						// need readLine method
		File stopword = new File(this.filePath_stopWordlist);
		try 
		{
			sReader = new BufferedReader(new FileReader(stopword));
			while((str = sReader.readLine()) != null )
			{					//the stopword.txt have 26 rows
				stoplist[count] = str.split(",");
				if(count >= spotwordRow)
				{
					throw new Exception("Row is over!");
				}
				count++;
			}
		} 
		catch (Exception e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private int _002_stringCompare(String word, String list[][])
	{		
		char temp [] = null;
		int lrow = 0;
		
		temp = word.toCharArray();						// charge the string letter to lower cast
		for(int i = 0;i < temp.length;i++){
			if(Character.isLetter(temp[i])){
				temp[i] = Character.toLowerCase(temp[i]);
			}
		}
		if(temp.length <= 0)							// if the word is empty
			return 0;
		
		lrow = (int)temp[0] - 97;						//choose the first letter to check, because the number of letter is 26   
		word = String.valueOf(temp);					//so we prepare 26 arrays, which correspond the order. like list[0] is a , list[2] is b ......
		if(lrow < 26 && lrow >=0){						//Because of that, if the word out of the 26 letters, we ignores it or add new array.
			for(int i = 0;i < list[lrow].length;i++){	//compare the stop word list in letter part
				if((word.equals(list[lrow][i])) == true)
					return 1;
			}
		}
		else{
			if((word.equals(",")) == true)		//compare ',' because this symbol is a split word 
				return 1;
			for(int i = 0;i < list[26].length;i++){		//compare the stop word list in symbol part
				if((word.equals(list[26][i])) == true)
					return 1;
			}
		}
		return 0;
	}
	private boolean _003_judgeContainSpecialWord(String word)
	{
		boolean isNotContain = true;
		int index = 0;
		while(isNotContain && index < word.length())
		{
			isNotContain = (word.charAt(index) >= 'A' && word.charAt(index) <= 'Z') ||
			  		       (word.charAt(index) >= 'a' && word.charAt(index) <= 'z') ||
					       (word.charAt(index) >= '0' && word.charAt(index) <= '9');
			index = index + 1;
		}
		return !isNotContain;
	}
	public boolean ACT001_judgeWordIsStopWord(String word_judge)
	{
		boolean isStopWord = false;
		if(word_judge.length() <= 2 || this._002_stringCompare(word_judge, this.stoplist) == 1 ||
		   this._003_judgeContainSpecialWord(word_judge))
		{
			isStopWord = true;
		}
		return isStopWord;
	}
	public static void main(String[] args) 
	{
		
	}
}

